package com.tcu.vod.service;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.tcu.config.MediaConfig;
import com.tcu.exception.SmartException;
import com.tcu.vod.entity.PlayInfo;
import com.tcu.vod.entity.VideoPlayAuth;
import com.tcu.vod.util.InitClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class AliyunVodImpl implements IVod{

    private MediaConfig config;
    @Autowired
    public void setConfig(MediaConfig config) {
        this.config = config;
    }

    @Override
    public String uploadVideo(MultipartFile file) {
        String videoId = null;
        String keyId = config.getKeyId();
        String keySecret = config.getKeySecret();

        String fileName = file.getOriginalFilename();
        String title = fileName.substring(0,fileName.indexOf("."));
        UploadStreamRequest request;
        try {
            request = new UploadStreamRequest(keyId,keySecret,title,fileName,file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            throw new SmartException(20001,"视频上传失败");
        }
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        if (response.isSuccess()) {
            videoId = response.getVideoId();
        }else{
            throw new SmartException(20001,"文件上传失败");
        }
        return videoId;
    }

    @Override
    public void deleteVideo(String videoId) {
        DefaultAcsClient client = InitClient.initVodClient(config.getKeyId(), config.getKeySecret());
//        delete request
        DeleteVideoRequest req = new DeleteVideoRequest();
        req.setVideoIds(videoId);

        try {
            DeleteVideoResponse response = client.getAcsResponse(req);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new SmartException(20001,"视频删除失败");
        }

    }

    @Override
    public PlayInfo getPlayInfo(String videoId) {
        PlayInfo info = null;
        DefaultAcsClient client = InitClient.initVodClient(config.getKeyId(),config.getKeySecret());
        GetPlayInfoRequest request =new GetPlayInfoRequest();
        request.setVideoId(videoId);

        try {
            GetPlayInfoResponse response = client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            info = new PlayInfo();
            GetPlayInfoResponse.PlayInfo playInfo = playInfoList.get(0);
            info.setRequestId(response.getRequestId());
            info.setPlayUrl(playInfo.getPlayURL());
            info.setTitle(response.getVideoBase().getTitle());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new SmartException(20001,"获得视频播放信息异常！");
        }
        return info;
    }

    @Override
    public VideoPlayAuth getVideoPlayAuth(String videoId) {
        DefaultAcsClient client = InitClient.initVodClient(config.getKeyId(),config.getKeySecret());
        VideoPlayAuth auth = new VideoPlayAuth();
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        try {
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            auth.setRequestId(response.getRequestId());
            auth.setPlayAuth(response.getPlayAuth());
            auth.setTitle(response.getVideoMeta().getTitle());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new SmartException(20001,"获取视频播放凭证失败！");
        }
        return auth;
    }
}
